<template>
    <div class="app-container calendar-list-container">
        <!-- 查询和其他操作 -->
        <div class="filter-container">
          <el-input clearable  style="width: 200px;" placeholder="平台编号" v-model="listQuery.platformId"></el-input>
          <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查询</el-button>
          <el-button class="filter-item" type="primary" v-if="topic_btn_add" @click="handleCreate" icon="el-icon-plus">添加</el-button>
        </div>

        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
          <el-table-column align="center" min-width="120px" label="平台编号" prop="platformId">
          </el-table-column>       
          <!-- <el-table-column align="center" min-width="100px" label="商户号" prop="merchantId">
          </el-table-column>        -->
          <el-table-column align="center" min-width="100px" label="标题" prop="title">
          </el-table-column>
          <el-table-column align="center" min-width="150px" label="内容" prop="content">
          </el-table-column>
          <el-table-column align="center" min-width="150px" label="描述" prop="discription">
          </el-table-column>
          <el-table-column align="center" min-width="150px" label="背景图片">
            <template slot-scope="scope">
                <img :src="scope.row.img" width="100"/>
            </template>
          </el-table-column>
          <el-table-column align="center" min-width="200px" label="发起时间" prop="createTime" sortable="">
          </el-table-column> 
          <el-table-column align="center" label="操作" width="150" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button type="danger" size="mini" v-if="topic_btn_update"  @click="handleUpdate(scope.row)">编辑</el-button>
                <el-button type="danger" size="mini" v-if="topic_btn_del"  @click="handleDelete(scope.row)">删除</el-button>
              </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
                :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>

        <!-- 添加对话框 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
          <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
            <el-form-item label="标题" prop="title">
              <el-input v-model="dataForm.title" clearable=""></el-input>
            </el-form-item>
            <el-form-item label="话题内容" prop="content">
              <el-input v-model="dataForm.content" clearable=""></el-input>
            </el-form-item>   
            <el-form-item label="背景图片" prop="img">
              <div  @click="upQrImg">
                <img v-if="dataForm.img" :src="dataForm.img">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </div>
              <input type="file" name="file" id="upload_qr" @change="uploadQr" accept="image/png,image/gif" style="display: none">
            </el-form-item>      
            <el-form-item label="描述" prop="discription">
              <el-input v-model="dataForm.discription" clearable=""></el-input>
            </el-form-item>           
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData('dataForm')">确定</el-button>
            <el-button v-if="dialogStatus == 'update'" type="primary" @click="updateData('dataForm')">确定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { listTopic, addTopic, deleteTopic, uploadImg, updateTopic } from '@/api/biz/topic/topic'
import { mapGetters } from 'vuex'

export default {
  name: 'topic',
  directives: {
    waves
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        platformId: ''
      },
      imFile: '',
      excelData: [],
      list: [],
      total: 0,
      listLoading: false,
      downloadLoading: false,
      dialogFormVisible: false,
      topic_btn_add: false,
      topic_btn_update: false,
      topic_btn_del: false,
      dataForm: {
        id: '',
        platformId: '',
        merchantId: '',
        title: '',
        content: '',
        discription: '',
        createBy: '',
        img: '',
        flag: ''
      },
      rules: {
        title: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        img: [
          {
            required: true,
            message: '请上传背景图片',
            trigger: 'blur'
          }
        ],
        content: [
          {
            required: true,
            message: '请输入介绍',
            trigger: 'blur'
          }
        ]
      },
      textMap: {
        create: '创建',
        update: '编辑'
      },
      dialogStatus: ''
    }
  },

  created() {
    this.getList()
    this.topic_btn_add = this.elements['topicManager:btn_add']
    this.topic_btn_del = this.elements['topicManager:btn_del']
    this.topic_btn_update = this.elements['topicManager:btn_update']
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'platformId'
    ])
  },

  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleDelete(row) {
      this.$confirm('此操作将删除选中话题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteTopic(row)
            .then(response => {
              console.log(response)
              if (response.code === '0000') {
                this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              }
            })
        })
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dataForm = row
      this.dialogFormVisible = true
    },
    upQrImg: function() {
      document.getElementById('upload_qr').click()
    },
    uploadQr(event) {
      if (event.target.files.length > 0) {
        this.file = event.target.files[0] // 提交的图片
        console.log(this.file)
        const param = new FormData() // 创建form对象
        param.append('file', this.file)
        uploadImg(param).then(response => {
          console.log(response)
          if (response.success) {
            this.dataForm.img = response.filePath
          }
        })
      }
    },
    getList() {
      this.listLoading = true
      this.listQuery.platformId = this.platformId
      listTopic(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },

    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData(formName) {
      this.dataForm.platformId = this.platformId
      //   this.dataForm.merchantId = this.merchantId
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addTopic(this.dataForm).then(response => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false
          updateTopic(this.dataForm).then(response => {
            if (response.code === '0000') {
              this.$notify({
                title: '成功',
                message: '修改成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.dataForm = {
        id: '',
        platformId: '',
        merchantId: '',
        title: '',
        content: '',
        discription: '',
        createBy: '',
        img: '',
        flag: ''
      }
    }
  }
}
</script>

<style>

</style>